package org.ihelpuoo.weboo.module;

import java.nio.charset.Charset;
import java.util.List;

import org.ihelpuoo.parser.segment.CharSegment;
import org.ihelpuoo.parser.segment.Segment;
import org.ihelpuoo.weboo.bean.ControllerBean;
import org.ihelpuoo.weboo.bean.ModuleBean;

public class ModuleLoader
{

	private ModuleBean	bean;
	private WebooConfig	webooConfig;

	public ModuleLoader(WebooConfig webooConfig, ModuleBean bean)
	{
		this.bean = bean;
		this.webooConfig = webooConfig;
	}

	public ModuleBean getBean()
	{
		return bean;
	}

	public String getActionHome()
	{
		return parseValueAsSegment(bean.getActionHome());
	}

	public String getStyleHome()
	{
		return parseValueAsSegment(bean.getStyleHome());
	}

	private String parseValueAsSegment(String s)
	{
		Segment seg = new CharSegment(s);
		seg.set("name",this.getName());
		seg.set("appRoot",webooConfig.getRootPath());
		return seg.toString();
	}

	public String getConfigFileName()
	{
		return parseValueAsSegment(bean.getConfigFile());
	}

	public List<ControllerBean> getControllerList()
	{
		return bean.getControllers();
	}

	public String getMessageFileName()
	{
		return parseValueAsSegment(bean.getMessageFile());
	}

	public String getName()
	{
		return bean.getName();
	}

	public Charset getInputCharset()
	{
		return Charset.forName(bean.getInputCharset());
	}

	public Charset getOuputCharset()
	{
		return Charset.forName(bean.getOutputCharset());
	}

	public ModuleInstaller getInstaller()
	{
		if (null == bean.getInstaller())
			return null;
		try
		{
			return (ModuleInstaller)Class.forName(bean.getInstaller()).newInstance();
		}
		catch (Exception e)
		{
		}
		return null;
	}

	public ModuleUninstaller getUninstaller()
	{
		if (null == bean.getUninstaller())
			return null;
		try
		{
			return (ModuleUninstaller)Class.forName(bean.getUninstaller()).newInstance();
		}
		catch (Exception e)
		{
		}
		return null;
	}

	public String getComment()
	{
		return bean.getComment();
	}

}
